module dgl.input.Mouse;

import derelict.sdl.mouse;

import dgl.Event;
import dgl.input.MouseEventArgs;
import dgl.input.MouseState;

static class Mouse
{	
	private static int m_curWheel;
	
	public static this()
	{
		m_curWheel = 0;
	}
	
	public static void setPosition( int x, int y )
	{
		
	}
	
	package static void onWheelUp()
	{
		m_curWheel++;
		wheelUp( null, new MouseEventArgs( Mouse.getState() ) );
	}
	
	package static void onWheelDown()
	{
		m_curWheel--;
		wheelDown( null, new MouseEventArgs( Mouse.getState() ) );
	}
	
	package static void onLeftButtonDown()
	{
		leftButtonDown( null, new MouseEventArgs( Mouse.getState() ) );
	}
	
	package static void onLeftButtonUp()
	{
		leftButtonUp( null, new MouseEventArgs( Mouse.getState() ) );
	}
	
	package static void onRightButtonDown()
	{
		rightButtonDown( null, new MouseEventArgs( Mouse.getState() ) );
	}
	
	package static void onRightButtonUp()
	{
		rightButtonUp( null, new MouseEventArgs( Mouse.getState() ) );
	}
	
	package static void onMiddleButtonDown()
	{
		middleButtonDown( null, new MouseEventArgs( Mouse.getState() ) );
	}
	
	package static void onMiddleButtonUp()
	{
		middleButtonUp( null, new MouseEventArgs( Mouse.getState() ) );
	}
	
	package static void onMouseMove()
	{
		mouseMove( null, new MouseEventArgs( Mouse.getState() ) );
	}
	
	public static MouseState getState()
	{
		int x, y;
		ubyte result;
		result = SDL_GetMouseState( &x, &y );
		ButtonState left, right, middle;
		left = result & SDL_BUTTON( SDL_BUTTON_LEFT ) ? ButtonState.Pressed : ButtonState.Released;
		right = result & SDL_BUTTON( SDL_BUTTON_RIGHT ) ? ButtonState.Pressed : ButtonState.Released;
		middle = result & SDL_BUTTON( SDL_BUTTON_MIDDLE ) ? ButtonState.Pressed : ButtonState.Released;
		MouseState ret = MouseState( x, y, left, middle, right, m_curWheel );
		return ret;
	}
	
	public static Event!( Mouse, MouseEventArgs ) leftButtonDown;
	
	public static Event!( Mouse, MouseEventArgs ) leftButtonUp;
	
	public static Event!( Mouse, MouseEventArgs ) rightButtonDown;
	
	public static Event!( Mouse, MouseEventArgs ) rightButtonUp;
	
	public static Event!( Mouse, MouseEventArgs ) middleButtonDown;
	
	public static Event!( Mouse, MouseEventArgs ) middleButtonUp;
	
	public static Event!( Mouse, MouseEventArgs ) wheelDown;

	public static Event!( Mouse, MouseEventArgs ) wheelUp;
	
	public static Event!( Mouse, MouseEventArgs ) mouseMove;
}